package com.funcas.pcloud.security.web;

import com.google.common.collect.Maps;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

/**
 * @author funcas
 * @version 1.0
 * @date 2018年10月18日
 */
@RestController
@RequestMapping("/auth")
public class DemoController {

    @PreAuthorize("hasAuthority('user:list')")
    @RequestMapping("/demo")
    public Object getDemo(){
        return "Hello , i am in secret";
    }

    @PreAuthorize("hasRole('USER')")
    @RequestMapping("/demo1")
    public Object getDemo1(){
        Object o = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        Map m = Maps.newHashMap();
        m.put("a", 121231313131L);
        m.put("principal", o);
        return m;
    }


    @GetMapping("/user")
    public Object user(){
        return SecurityContextHolder.getContext().getAuthentication().getPrincipal();
    }
}
